package com.chinawayltd.ews.sample.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.DateUtil;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 智能任务调度时间格式化工具类
 * @program: greeProject
 * @description:
 * @author: xiazj
 * @create: 2018-05-08 16:22
 **/
public class TaskDispathDateUtils {

    private static final Integer SCALE = 12;


    /**
     * 把数字类型的时间转化成日期对象
     * Example： 42795.583333333336 -> 2018-03-01 14:00:00
     * @param value
     * @return
     */
    public static Date formatCustomDate(Double value){
//        Date result = null;
//        if(value != null && value > 0){
//            Double time = (value - 25204) * 24 * 3600 - 8 * 3600;
//            result = new Date(time.longValue() * 1000);
//        }
        Date result = DateUtil.getJavaDate(value);
        return result;
    }

    /**
     * 把数字类型的时间转化成日期字符串
     * Example： 42795.583333333336 -> 2018-03-01 14:00:00
     * @param value
     * @pattern SimpleDateFormat的日期格式化格式
     * @return
     */
    public static String formatCustomDate(Double value, String pattern){
        String result = null;
        if(value != null && value > 0){
            Date date = formatCustomDate(value);
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            result = sdf.format(date);
        }
        return result;
    }

    /**
     * 日期字符串转化成数字类型的时间格式
     *  Example： 2018-03-01 14:00:00 -> 42795.583333333336
     * @param dateStr
     * @param pattern SimpleDateFormat的日期格式化格式
     * @return
     * @throws ParseException
     */
    public static Double parseCustomDate(String dateStr, String pattern) throws ParseException {
        Double result = null;
        if(StringUtils.isNotBlank(dateStr)){
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date date = sdf.parse(dateStr);
            result = parseCustomDate(date);
        }
        return result;
    }

    /**
     * 日期对象转化成数字类型的时间格式
     *  Example： 2018-03-01 14:00:00 -> 42795.583333333336
     * @param date
     * @return
     * @throws ParseException
     */
    public static Double parseCustomDate(Date date) throws ParseException {
//        Double result = null;
//        if(date != null){
//            Double time = new Double(date.getTime());
////            time = (time / 1000 + 8 * 3600) / 24 / 3600 + 25204 ;
//            BigDecimal divisor = new BigDecimal(time / 1000 + 8 * 3600);
//            BigDecimal dividend = new BigDecimal(24 * 3600);
//            BigDecimal value = divisor.divide(dividend, SCALE, BigDecimal.ROUND_HALF_DOWN);
//            result = value.doubleValue() + 25204;
//        }
        Double result = DateUtil.getExcelDate(date);
        return result;
    }

    /**
     * 时间字符串转化成数字类型的时间格式
     * Example： 23:33-> 0.98125
     * @param timeStr 格式：hh:ss
     * @return
     */
    public static Double parseCustomTime(String timeStr){
        Double result = null;
        if(StringUtils.isBlank(timeStr)){
            throw new RuntimeException("timeStr must be not null!");
        }
        Pattern pattern = Pattern.compile("^[0-9]{2}:[0-9]{2}$");
        Matcher matcher = pattern.matcher(timeStr);
        if(!matcher.matches()){
            throw new RuntimeException("timeStr 格式不正确，正确格式：hh:ss");
        }
//        String[] timeStrSplit = timeStr.split(":");
//        Integer hour = Integer.parseInt(timeStrSplit[0]);
//        Integer minute = Integer.parseInt(timeStrSplit[1]);
//        BigDecimal divisor = new BigDecimal(hour * 60 + minute);
//        BigDecimal dividend = new BigDecimal(24 * 60);
//        BigDecimal value = divisor.divide(dividend, SCALE, BigDecimal.ROUND_HALF_DOWN);
//        result = value.doubleValue();
        result = DateUtil.convertTime(timeStr);
        return result;
    }

    public static void main(String[] args) throws ParseException {
        String pattern = "yyyy-MM-dd HH:mm:ss";
//        double x = 42883.23958333348;
        double x = 43252;
        String dateStr = TaskDispathDateUtils.formatCustomDate(x, pattern);
        Double dateNums = TaskDispathDateUtils.parseCustomDate(dateStr, pattern);
        System.out.println("sourse:" + x);
        System.out.println("dateStr:" + dateStr);
        System.out.println("dateNums:" + dateNums);
        String aaa = "03:00";
        System.out.println(TaskDispathDateUtils.parseCustomTime(aaa));

        System.out.println(Math.ceil(3.277));
    }

}
